using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.Resourceful;

namespace CodePlex.SpaceBlock
{
    public static class StringMarshaller
    {
        public static object[] FromString(string[] values)
        {
            object[] rt = new object[values.Length];

            for (int i = 0; i < rt.Length; i++)
            {
                rt[i] = StringMarshaller.FromString(values[i]);
            }

            return rt;
        }

        public static string[] ToString(object[] values)
        {
            string[] rt = new string[values.Length];

            for (int i = 0; i < rt.Length; i++)
            {
                rt[i] = StringMarshaller.ToString(values[i]);
            }

            return rt;
        }

        public static object FromString(string value)
        {
            int i = value.IndexOf('|');

            string name = value.Substring(0, i);

            string data = value.Substring(i + 1);

            if (name == "Path")
            {
                return Path.Parse(data);
            }
           
            throw CodePlexException.Format(string.Format("Cannot parse value: {0}",value));
        }

        public static string ToString(object value)
        {
            Path path = value as Path;

            if (path != null)
            {
                return string.Format("Path|{0}", path);
            }

            IPathItem pathItem = value as IPathItem;

            if (pathItem != null)
            {
                return string.Format("Path|{0}", pathItem.Path);
            }

            throw CodePlexException.Format("Cannot stringify value: {0}", Convert.ToString(value));
        }

    }
}
